package com.minshenglife.zanzan.web.weixin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.minshenglife.zanzan.entity.AccessTokenAndJsapiTicket;
import com.minshenglife.zanzan.pub.Config;
import com.minshenglife.zanzan.pub.utils.AesSecretTool;
import com.minshenglife.zanzan.pub.utils.Message;
import com.minshenglife.zanzan.service.AccessTokenAndJsapiTicketService;


/**
 * 
 * @author 王树志
 * 
 */
@Controller
@RequestMapping("/weijia")
public class JsapiTicketController extends BaseController {
	Logger log = LoggerFactory.getLogger(JsapiTicketController.class);
	@Resource
	AccessTokenAndJsapiTicketService service;
	
	/**
	 * 获取JSAPI_TICKET
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getTicket.do", method = RequestMethod.GET)
	@ResponseBody
	public Message getJsapiTicket(HttpServletRequest request, HttpServletResponse response) {
		Message message = new Message(Message.STATE_one);
		try {
			AccessTokenAndJsapiTicket ticket = service.getAccessTokenAndJsapiTicket(Config.getProperty("WX_APPID"));
			if(ticket!=null){
				message.setState(Message.STATE_200);
				String ticketStr = ticket.getJsapiTicket();
				//对jsapi_ticket加密
				ticketStr = AesSecretTool.AesEncrypt(ticketStr,Config.getProperty("TICKET_KEY"));
				message.setMsg(ticketStr);
				log.info("获取jsapi_ticket成功=="+ticketStr);
			}else{
				message.setMsg("获取参数失败！");
				log.error("获取jsapi_ticket失败！！");
			}
			log.info(message.toString());
		} catch (Exception e) {
			e.printStackTrace();
			message = new Message(Message.STATE_one, "服务器异常");
			log.debug(message.toString());
		}
		return message;
	}
}
